var configDialog = dialogs.build({
    title: "设置打卡时间",
    customView:
        <vertical>
            <text text="小时：" textSize="16sp"/>
            <input id="hourInput" inputType="number" hint="小时"/>
            <text text="分钟：" textSize="16sp"/>
            <input id="minuteInput" inputType="number" hint="分钟"/>
        </vertical>,
    positive: "确定",
    negative: "取消"
});


let time = 0
configDialog.on("positive", () => {

    var hour = parseInt(configDialog.customView.hourInput.text());
    var minute = parseInt(configDialog.customView.minuteInput.text());
    var now = new Date();
    if (!isNaN(hour) && !isNaN(minute) && hour >= 0 && hour < 24 && minute >= 0 && minute < 60) {
        var targetTime = new Date(now.getFullYear(), now.getMonth(), now.getDate(), hour, minute, 0, 0).getTime();
        time = targetTime
        toast("已设置打卡时间为 " + hour + ":" + minute);
    } else {
        toast("请输入有效的小时和分钟");
    }
});
configDialog.show();

let isFinish = false
setInterval(function () {
    var now = new Date();
    var currentTime = now.getTime();
    var targetTime = time;

    var packageName = "com.beisen.italent";
    if (!isFinish && currentTime >= targetTime&&time!==0) {
        var intent = context.getPackageManager().getLaunchIntentForPackage(packageName);
        if (intent != null) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        } else {
            toast("未找到该应用！");
        }
        isFinish=true
    }

}, 1000);
